package daydayup;

import java.util.Deque;
import java.util.LinkedList;

public class Q71 {

    public static void main(String[] args) {
        String str = "/a/./b/../../c/";
        System.out.println(simplifyPath(str));

    }

    public static String simplifyPath(String path) {
        //栈   双向队列
        Deque<String> stack = new LinkedList<>();
        String[] arr = path.split("/");
        for (String str: arr) {
            if (str.equals("..") && !stack.isEmpty()) stack.pop();
            else if (!str.equals("") && !str.equals("..") && !str.equals(".")) stack.push(str);
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            sb.insert(0, stack.pop());
            sb.insert(0, "/");
        }
        if (sb.length() == 0) sb.append("/");
        return sb.toString();
    }
}
